<template>
  <div class="user-manage-container">
    <div class="app-header">
      <h1 class="app-title">用户管理</h1>
    </div>
    
    <div class="main-card">
      <div class="action-buttons">
        <button class="btn query-btn" @click="handleQuery">
          <span class="btn-icon">🔍</span>
          查询
        </button>
        <button class="btn add-btn" @click="showCreate = true">
          <span class="btn-icon">➕</span>
          新增
        </button>
      </div>
      
      <UserList 
        :users="users" 
        @edit-user="handleEditUser"
        @refresh="fetchUsers"
      />
    </div>

    <!-- 编辑用户弹窗 -->
    <UserEdit
      v-if="showEdit"
      :user="editingUser"
      @close="showEdit = false"
      @user-updated="handleUserUpdated"
    />

    <!-- 新增用户弹窗 -->
    <UserCreate
      v-if="showCreate"
      @close="showCreate = false"
      @user-created="handleUserCreated"
    />
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import UserList from '../components/UserList.vue'
import UserEdit from '../components/UserEdit.vue'
import UserCreate from '../components/UserCreate.vue'
import { apiService } from '../utils/http'
import { tools } from '../utils/tools'

export default {
  name: 'UserManage',
  components: {
    UserList,
    UserEdit,
    UserCreate
  },
  setup() {
    const users = ref([])
    const showEdit = ref(false)
    const showCreate = ref(false)
    const editingUser = ref(null)

    const fetchUsers = async () => {
      try {
        const headers = {token: `${tools.getTokenFromCookie()}`}
        const { res, isAuthenticated } = await tools.authApiCall(
          () => apiService.post('/UserQuery', {username: ''}, { headers }),
          useRouter(),
          useRoute()
        )
        users.value = res.data.data
      } catch (error) {
        console.error('查询用户失败:', error)
      }
    }

    const handleQuery = () => {
      fetchUsers()
    }

    const handleEditUser = (user) => {
      editingUser.value = { ...user }
      showEdit.value = true
    }

    const handleUserUpdated = () => {
      showEdit.value = false
      fetchUsers()
    }

    const handleUserCreated = () => {
      showCreate.value = false
      fetchUsers()
    }

    onMounted(() => {
      fetchUsers()
    })

    return {
      users,
      showEdit,
      showCreate,
      editingUser,
      handleQuery,
      handleEditUser,
      handleUserUpdated,
      handleUserCreated,
      fetchUsers
    }
  }
}
</script>

<style scoped>
.user-manage-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f0f4ff 0%, #f8f0ff 100%);
  padding: 16px;
}

.app-header {
  text-align: center;
  margin-bottom: 24px;
}

.app-title {
  color: #7b68ee;
  font-size: 24px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(123, 104, 238, 0.1);
}

.main-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(123, 104, 238, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.action-buttons {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
}

.btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 20px;
  border: none;
  border-radius: 16px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.query-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.add-btn {
  background: linear-gradient(135deg, #ff6b6b 0%, #ff8e8e 100%);
  color: white;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

.btn:active {
  transform: translateY(0);
}

.btn-icon {
  font-size: 18px;
}

@media (max-width: 480px) {
  .user-manage-container {
    padding: 12px;
  }
  
  .app-title {
    font-size: 20px;
  }
  
  .main-card {
    padding: 16px;
    border-radius: 16px;
  }
  
  .btn {
    padding: 12px 16px;
    font-size: 14px;
  }
}
</style>
